package com.itheima.interceptor;

import com.itheima.util.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (ObjectUtils.isEmpty(token) || JwtUtils.parseJwt(token) == null) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return false;
        }
        //Map<String, Object> map = JwtUtils.parseJwt(token);
        //CurrentHolder04.setLocal(Integer.parseInt(map.get("id").toString()));
        return true;

    }

    /*@Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String token = request.getHeader("token");
        if (!ObjectUtils.isEmpty(token) && JwtUtils.parseJwt(token) != null) {
            CurrentHolder04.removeLocal();
        }
        log.info("ThradLocal变量销毁完成");
    }*/
}
